/*
 *  Copyright (C) 2011 glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.commons.listeners;

import java.util.ArrayList;
import java.util.List;
import org.balisunrise.common.Action;
import org.balisunrise.common.Event;

/**
 *
 * @author Glauco
 */
public class TransactionListener 
        implements org.balisunrise.common.listeners.TransactionListener{

    private List<Action> bv;
    private List<Action> bs;
    private List<Action> as;
    private List<Action> bd;
    private List<Action> ad;

    public TransactionListener() {
        bv = new ArrayList<Action>();
        bs = new ArrayList<Action>();
        as = new ArrayList<Action>();
        bd = new ArrayList<Action>();
        ad = new ArrayList<Action>();
    }

    public void addBeforeValidadeAction(Action action){
        if(bv.contains(action))
            return ;
        bv.add(action);
    }

    public void addBeforeSaveAction(Action action){
        if(bs.contains(action))
            return ;
        bs.add(action);
    }

    public void addAfterSaveAction(Action action){
        if(as.contains(action))
            return ;
        as.add(action);
    }

    public void addBeforeDeleteAction(Action action){
        if(bd.contains(action))
            return ;
        bd.add(action);
    }

    public void addAfterDeleteAction(Action action){
        if(ad.contains(action))
            return ;
        ad.add(action);
    }

    @Override
    public void beforeValidade(Event event) {
        for (Action action : bv) {
            action.perform(event);
        }
    }

    @Override
    public void beforeSave(Event event) {
        for (Action action : bs) {
            action.perform(event);
        }
    }

    @Override
    public void afterSave(Event event) {
        for (Action action : as) {
            action.perform(event);
        }
    }

    @Override
    public void beforeDelete(Event event) {
        for (Action action : bd) {
            action.perform(event);
        }
    }

    @Override
    public void afterDelete(Event event) {
        for (Action action : ad) {
            action.perform(event);
        }
    }

}
